#conding:utf-8
from util.operate_excel import OperationExcel
from base.runmethod import RunMethod
from data.get_data import GetData
from jsonpath_rw import jsonpath,parse
import json
import demjson
class DependentData:
    def __init__(self):
    #     初始化
    # def __init__(self,case_id):
        # self.case_id=case_id
        self.opre_excel=OperationExcel()
        self.data=GetData()
        # self.runmethod=RunMethod()
    # 通过caseid去获取整行数
    # def get_case_line_data(self,case_id):
    #     r=self.opre_excel.get_row_data(case_id)
    #     return r

    # 通过case_id去获取整行数据
    def get_case_line_data(self,case_id):
        # rows_data = self.opre_excel.get_row_data("test01")
        rows_data=self.opre_excel.get_row_data(case_id)
        return rows_data

    #执行依赖测试，获取结果
    def run_dependent(self,case_id):
        run_method=RunMethod()
        # 获取caseid的行数
        row_num=self.opre_excel.get_row_num(case_id)
        # row_num=self.opre_excel.get_row_num(self.case_id)
        # 获取请求参数data里面
        request_data=self.data.get_data_for_json(row_num)
        # 获取表头
        header=self.data.is_header(row_num)
        # 获取请求方式
        method=self.data.get_request_method(row_num)
        # 获取url
        url=self.data.get_request_url(row_num)
        # 运行请求并返回数值赋值给res
        res=run_method.run_main(method,url,request_data,header)
        # print(res)
        # print(json.dumps(res))
        res=json.dumps(res)
        print("232",res)
        # 返回数据
        # print(json.dumps(res.text))
        # return res.text
        return res

    # # 通过根据依赖数据key去获取执行依赖测试case的响应，然后返回,传获取关联参数，与保存地址,depend_id是执行数据关联参数，save_value保存的值
    # def get_data_for_key(self,row,case_id,depend_id,save_value):
    #     # 获取依赖数据字段
    #     depend_data = self.data.get_depend_key(row)
    #     # 获取运行case_id返回接口返回json格式数据
    #     response_data = self.run_dependent(case_id)
    #     # 把json格式转换为对象
    #     response_dat=json.loads(response_data)
    #     print("****" + response_dat[depend_data])
    #     # 返回获取结果
    #     return response_dat[depend_data]

    # 111111111111111111111111111
    # 通过根据依赖数据key去获取执行依赖测试case的响应，然后返回,传获取关联参数，与保存地址,depend_id是执行数据关联参数，save_value保存的值
    def get_data_for_key(self,row,case_id):
        # save_id_test=save_value
        # print(save_id_test)
        # 获取保存值

        # 获取依赖数据字段
        depend_data = self.data.get_depend_key(row)
        print("depend_data",depend_data)
        # 获取运行case_id返回接口返回json格式数据
        response_data = self.run_dependent(case_id)
        response_data=json.loads(response_data)
        print("response_data",response_data[depend_data])
        # 把json格式转换为对象
        # response_dat=json.loads(response_data)
        # response_dat =  response_data
        # print("****" + response_data['data'])
        # 写入进json中
        # save_id = "nottoken"
        # save_i = "Authorization"

        path =r"D:\jiekkou\dataconfig\tets.json"
        with open(path, 'r') as fr:
            # 打卡json文件
            json_all = json.load(fr)
            print("++",type(json_all))
            # 赋值
            # print("213", save_valusave_id_teste)
            # json_all['token']['Authorization'] = response_data[depend_data]
            # json_all[save_value]="1"
            print("21",depend_data)
            # json_all['nottoken']['Authorization']=response_data[depend_data]
            # json_all[save_value]=response_data[depend_data]


            # json_all['token']['Authorization'] = response_data[depend_data]
            # json_all['token']['Authorization']=response_dat[depend_data]
            # 把结果插入json文件中['token']['Authorization']字段中
            with open(path, "w") as fw:


                # test_id="['nottoken']['Authorization']"
                # json_all.response_data[depend_data]
                # json_all[save_id][save_i]=response_data[depend_data]
                # json_all['nottoken']['Authorization']=response_data[depend_data]
                json_all[save_id]=response_data[depend_data]
                json.dump(json_all, fw ,ensure_ascii=False, indent=4)

        # 返回获取结果
        # return response_data[depend_data]



    # 写入依赖附属数据
    # 写入表头的token
    def write_header_token(self,row,case_id):
        # 从依赖数据中获取对应的token的值
        token=self.get_data_for_key(row,case_id)
        # json文件路径
        path = r"D:\jiekkou\dataconfig\tets.json"
        # 读取json文件
        with open(path, 'r') as fr:
            # 打卡json文件
            json_all = json.load(fr)
            # 赋值
            json_all['token']['Authorization']=token
            # 把结果插入json文件中['token']['Authorization']字段中
            with open(path, "w") as fw:
                json.dump(json_all, fw, ensure_ascii=False, indent=4)





    #通过根据依赖数据key去获取执行依赖测试case的响应，然后返回
    def get_data_for_keytest(self, row, case_id):
        dat = 'data'
        ## 获取依赖数据
        depend_data = self.data.get_depend_key(row)
        # print(depend_data)
        # 获取返回结果
        response_data = self.run_dependent(case_id)
        t = json.loads(response_data)

    def get_data_for_key_token(self,row,case_id):
        # 定义返回数据为data里面取值
        dat='data'
        ## 获取依赖数据
        depend_data=self.data.get_depend_key(row)
        # print(depend_data)
        #获取返回结果
        response_data=self.run_dependent(case_id)
        t=json.loads(response_data)
        # print(t)
        print(t[dat])

        # 加token
        path = r"D:\jiekkou\dataconfig\tets.json"
        with open(path, 'r') as fr:
            json_all = json.load(fr)
            json_all['token']['Authorization'] =t[dat]
            with open(path, "w") as fw:
                json.dump(json_all, fw, ensure_ascii=False, indent=4)

        # 正则提取
        # json_exe=parse(t)
        # madle=json_exe.find(t)
        # # print(madle)
        # print([type(math.value) for math in madle][0])

    def write_token(self,row,case_id,token):
        depend_data = self.data.get_depend_key(row)
        # print(depend_data)
        # 获取返回结果
        response_data = self.run_dependent(case_id)
        t = json.loads(response_data)
        path = r"D:\jiekkou\dataconfig\tets.json"
        with open(path, 'r') as fr:
             json_all = json.load(fr)
             json_all['token']['Authorization'] =t[token]
             with open(path, "w") as fw:
                 json.dump(json_all, fw, ensure_ascii=False, indent=4)
    def get_token(self,row,case_id):
        ## 获取依赖数据
        depend_data = self.data.get_depend_key(row)
        # print(depend_data)
        # 获取返回结果
        response_data = self.run_dependent(case_id)
        tt=json.loads(response_data)
        # print(tt['data'])

    # 获取是否保存token
    def write_headers_token(self,row,response_data):
        # print(response_data)
        # 获取token是否保存值
        token_status=self.data.get_write_token(row)
        # print(token_status)
        # print("##",type(response_data))
        if token_status =="yes" :
            # print(type(response_data))
            # print(response_data['data'])
            path = r"D:\jiekkou\dataconfig\tets.json"
            with open(path, 'r') as fr:
                # 打卡json文件
                json_all = json.load(fr)
                # 赋值，返回字段response_data中的取['data']进行赋值
                json_all['token']['Authorization'] = response_data['data']
                # 把结果插入json文件中['token']['Authorization']字段中
                with open(path, "w") as fw:
                    json.dump(json_all, fw, ensure_ascii=False, indent=4)
        # 否则返回true
        else:
            return True
        # 否则返回true
    def write_result(self, row, data):
        self.opre_excel.write_result_excel(row, data)
    # 判断返回status是否为200并返回结果
    def expected_result(self,row,data):
        print("test")
    def write_dependent_data(self,row,path,value,case_id):
        print("1213")





if  __name__=='__main__':
    oper=DependentData()
    # save_id ="['Authorization']"
    save_id="['nottoken']['Authorization']"
    # save_id="[1][2]"
    oper.get_data_for_key(1,'test01',save_id)
    # oper.get_token(1,'test01')
    # oper.get_data_for_key_token(1,'test01')
    # dat={"status":200,"message":"success","data":"1eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIyODQiLCJ1c2VySWQiOiIyMDA1MTM0OTAyMTYyMzAwMDEiLCJuYW1lIjoibW9iaWxlIiwiZXhwIjozNzg3MTk2ODQ5fQ.QPetsF1ln7iKkXGdjBpKInxSQ-wZjVaJXvQNhj7qujDnmqZghuUI_umIXfj2j352DEdpT9YTPDTNVBTLdQM0WkHC9Rh4tWrJfoiZe9tnrHHnVnOWJ3ui6zIUaKBc9OUlWhdYC8La58bgchTTR8CA5oMRRjw_8GJUbg-SUREQYFk"}
    # oper.write_headers_token(1,dat)
    # oper.write(1,str(dat))



